<?php
    
    include_once('../src/classes/class.mysql.php');
    
    session_start();
    
    $comtec_id_order = $_GET["id_order"];
    
    $db = new MySQL();
    
    $tsql = "
             SELECT m.comtec_id_machine,o.cod_order,op.comtec_id_operation AS codigo, op.operation_type AS tipo,brand AS marca,
                   CONCAT_WS(' ',r.name,r.lastname1,r.lastname2) AS encargado, ow.company_name AS propietario,
                   DATE_FORMAT(op.operation_date_end,'%d/%m/%Y') AS fecha,
                    m.cod_mainteiner,m.model,s.name_s
             FROM  (((((orders AS o
                       INNER JOIN operation AS op ON op.comtec_id_order = o.comtec_id_order)
                       INNER JOIN responsible AS r ON op.comtec_id_responsible = r.comtec_id_responsible)
                       INNER JOIN machines AS m ON op.comtec_id_machine = m.comtec_id_machine)
                       INNER JOIN owner AS ow ON m.comtec_id_owner = ow.comtec_id_owner)
                       INNER JOIN status_op as s ON s.id_s = op.comtec_id_status)
             WHERE op.comtec_id_order=$comtec_id_order AND op.status_reg=1
             ORDER BY op.operation_date_end DESC
            ";
    
    $stmt = $db->query($tsql);       
    
    $texto = "<table class='table'><tr>";
    
    if($_SESSION["users"] == "managers"){
     $texto .= "<th colspan=2></th>";
    }else{ if($_SESSION["users"] != "clients") { $texto .= "<th></th>";}}            
    
    $texto .= "<th>C&oacute;digo</th>
               <th>Tipo</th>
               <th>C&oacute;d. m&aacute;quina</th>
               <th>Marca</th>
               <th>Modelo</th>
               <th>Propietario</th>
               <th>Encargado</th>
               <th>Fecha oper.</th>
               <th>C&oacute;d. orden</th>";
    if(strcmp($_SESSION["users"],"clients")!=0){
     $texto .= "<th>Estado</th>";
    }
    $texto .= "</tr>";  
    $cont = 0;
    
    while ($row = mysql_fetch_array($stmt, MYSQL_ASSOC)){
     $cont += 1;
     
     $comtec_id_machine=$row['comtec_id_machine'];
     $Cod_Order=$row['cod_order'];
     $Codigo=$row['codigo'];   
     $Tipo=$row['tipo'];
     $Marca=$row['marca'];
     $Encargado=$row['encargado'];
     $Propietario=$row['propietario'];
     $Fecha=$row['fecha'];
     $cod_mainteiner=$row['cod_mainteiner'];
     $Modelo=$row['model'];
     $status=$row['name_s'];
     
     $texto .= "<tr>";
     if($_SESSION["users"]!="clients"){
     $url_edit='http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
     $texto .= "<td><a href='javascript:loadOperation(".$Codigo.");'><img src='$url_edit' alt='editar' style='border: 0px;' /></a></td>";
     }if($_SESSION["users"]=="managers"){$url_delete='http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
     $texto .= "<td><a href='javascript:deleteOperation(".$Codigo.");'><img src='$url_delete' alt='eliminar' style='border: 0px;' /></a></td>";}
     $texto .= "<td><a href='javascript:operation(".$Codigo.");'>$Codigo</a></td>";
     switch ($Tipo) {
      case 1: 
             $Tipo = "Mantenci&oacute;n";
      break;
      case 2: 
             $Tipo = "Emergencia";
      break; 
      case 3: 
             $Tipo = "Instalaci&oacute;n";
      break;
      case 4: 
             $Tipo = "Reparaci&oacute;n";
      break;
     }
     $texto .= "<td>$Tipo</td>";
     $texto .= "<td>$cod_mainteiner</td>";
     $texto .= "<td>$Marca</td>";
     $texto .= "<td>$Modelo</td>";
     $texto .= "<td>$Propietario</td>";
     $texto .= "<td>$Encargado</td>";   
     $texto .= "<td>$Fecha</td>";
     $texto .= "<td>$Cod_Order</td>";
     if($_SESSION["users"]!="clients"){
     $texto .= "<td>$status</td>";}
     $texto .= "</tr>";
    }
    $texto .= "</table>";
    $texto .= '<div id="result"></div>';
    echo "<p class='total'>Total: $cont</p>".$texto;
?>
